package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.login.GenAreaBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Area;
import com.zhuss.common.AreaSelectVo;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 创建用户 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("areaGen")
public class AreaGen extends AbstractProcessor<List<Area>, GenAreaBo> implements Processor<List<Area>, GenAreaBo> {
	
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	
	@Autowired
	@Qualifier("partRead")
	private PartRead partRead;
	
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	

    /**
     * 校验
     */
    @Override
    public void check(GenAreaBo param) throws WpsException {
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
	@Override
	@SuppressWarnings("unchecked")
    public List<Area> process(GenAreaBo param) throws WpsException {
    	Long userId = param.getUserId();
    	List<Area> result = new ArrayList<Area>();
    	List<AreaSelectVo> areaList = (List<AreaSelectVo>) partRead.areaList2();
    	for (AreaSelectVo sv : areaList) {
    		String pic = sv.getTag();
    		Area area = new Area();
    		area.setId(sv.getId());
    		area.setName(sv.getName());
    		DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(pic);
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			List<String> urlList = downloadVo.getUrlList();
    		area.setPic(urlList);
    		area.setLat(sv.getLat());
    		area.setLon(sv.getLon());
    		area.setContent(sv.getContent());
    		area.setCreateTime(sv.getCreateTime());
    		result.add(area);
    	}
    	DetailUserVo user = (DetailUserVo) userRead.detail(userId);
    	Integer area = user.getArea();
    	Integer isLimitArea = user.getIsLimitArea();
    	if (IsEnum.是.sameKey(isLimitArea)) {
    		for (int i = result.size() - 1; i >= 0; i--) {
    			Area a = result.get(i);
    			if (a.getId().equals(area)) {
    				result.clear();
    				result.add(a);
    				break;
    			}
    		}
    	}
    	return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public List<Area> log(GenAreaBo param) throws WpsException {
		Long userId = param.getUserId();
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户区域列表获取.key());
    	record.setTargetId(userId);
    	record.setTargetName(userId + "");
    	record.setContent("参数: " + JsonUtil.toJson(userId));
    	record.setReason(userId + "");
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
